package com.mysiteforme.admin.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mysiteforme.admin.config.RestTemplateConfig;
import com.mysiteforme.admin.entity.Greeting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@EnableAutoConfiguration
@Import(value = {RestTemplateConfig.class})
@RequestMapping("/springrest")
public class SpringRestTemplateApp {

    @Autowired
    RestTemplate restTemplate;


    /***********HTTP GET method*************/
    @RequestMapping("zz")
    public String hellozz(){
        Greeting quote = restTemplate.getForObject("http://rest-service.guides.spring.io/greeting", Greeting.class);
        System.out.print(quote.getId() + "  " + quote.getContent());
        return quote.getId() + "  " + quote.getContent();
    }


    /***********HTTP GET method*************/
    @RequestMapping("")
    public String hello(){
        String url = "http://rest-service.guides.spring.io/greeting";
        JSONObject json = restTemplate.getForEntity(url, JSONObject.class).getBody();
        return json.toJSONString();
    }

    @RequestMapping("/json")
    public Object genJson(){
        JSONObject json = new JSONObject();
        json.put("descp", "this is spring rest template sample");
        return json;
    }

    @RequestMapping("/hello")
    public String hello1(){
        String url = "http://rest-service.guides.spring.io/greeting";
        JSONObject json = restTemplate.getForEntity(url, JSONObject.class).getBody();
        return json.toJSONString();
    }

    /**********HTTP POST method**************/
    @RequestMapping("/postApi")
    public Object iAmPostApi(@RequestBody JSONObject parm){
        System.out.println(parm.toJSONString());
        parm.put("result", "hello post");
        return parm;
    }

    @RequestMapping("/post")
    public Object testPost(){
        String url = "http://localhost:8080/postApi";
        JSONObject postData = new JSONObject();
        postData.put("descp", "request for post");
        JSONObject json = restTemplate.postForEntity(url, postData, JSONObject.class).getBody();
        return json.toJSONString();
    }


    @RequestMapping("/fccrepacktowers")
    public Object FccRepackTowers(){
        String url = "https://antennaweb.org/api/Prediction/FccRepackTowers";
        JSONObject postData = new JSONObject();
        postData.put("Latitude", 42.65287);
        postData.put("Longitude", -73.74868);
        postData.put("ReceiveHeight", "6.0");
        postData.put("IsObstructed", "false");
//        {"Latitude":42.65287,"Longitude":-73.74868,"ReceiveHeight":"6.0","IsObstructed":"false"}
        JSONArray json = restTemplate.postForEntity(url, postData, JSONArray.class).getBody();
        return json;
    }

    @RequestMapping(value = "/programs")
    @ResponseBody
    public Object Programs(){
        String url = "https://antennaweb.org/api/Prediction/Programs";
        JSONObject postData = new JSONObject();
//        postData.put("phase", 0);
//        postData.put("phaseCompletionDate", "");
//        postData.put("goingDark", false);
//        postData.put("channelChanging", "false");
//        postData.put("oldRfChannel", 12);
//        postData.put("coronadoStationId", 691);
//        postData.put("callSign", "WNYT-DT");
//        postData.put("parentCallSign", "WNYT-DT");
//        postData.put("network", "NBC");
//        postData.put("networkLogo", "");
//        postData.put("ReceiveHeight", "6.0");
//        postData.put("IsObstructed", "false");
//        postData.put("Latitude", 42.65287);
//        postData.put("Longitude", -73.74868);
//        postData.put("ReceiveHeight", "6.0");
//        postData.put("IsObstructed", "false");

        String string = "        {\n" +
                "            \"phase\": 0,\n" +
                "                \"phaseCompletionDate\": \"\",\n" +
                "                \"goingDark\": false,\n" +
                "                \"channelChanging\": false,\n" +
                "                \"oldRfChannel\": 12,\n" +
                "                \"coronadoStationId\": 691,\n" +
                "                \"callSign\": \"WNYT-DT\",\n" +
                "                \"parentCallSign\": \"WNYT-DT\",\n" +
                "                \"network\": \"NBC\",\n" +
                "                \"networkLogo\": \"\",\n" +
                "                \"rfChannel\": 12,\n" +
                "                \"psipChannel\": \"13.1\",\n" +
                "                \"distanceInMiles\": 13.412939739926026,\n" +
                "                \"directionMagnetic\": 275.4292673461789,\n" +
                "                \"directionTrue\": 261.92027991942109,\n" +
                "                \"latitude\": 42.625277099999991,\n" +
                "                \"longitude\": -74.00999999999857,\n" +
                "                \"antennaColor\": \"Red\",\n" +
                "                \"channelCount\": 1\n" +
                "        }";

        postData  = JSONObject.parseObject(string);


        JSONArray json = restTemplate.postForEntity(url, postData, JSONArray.class).getBody();
        return json;
    }


    public static void main(String[] args) throws Exception {
        SpringApplication.run(SpringRestTemplateApp.class, args);
    }

}